<?php

use Biz\service\trade\Order as OrderService;
use Doraemon\tools\ActionLog;

class ConfirmAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REALNAME;

    public function main()
    {
        //todo 版本判断



        $json_param = $this->getRequest()->getPost('json_param', '');
        if ($json_param) {
            $json_param_arr = !empty($json_param) ? json_decode($json_param, true) : [];
            $submitSku = $json_param_arr['sku_list'] ?? [];
            $source = $json_param_arr['source'] ?? 1;
        } else {
            $skuList = $this->getRequest()->getPost('sku_list', '');
            $source = $this->getRequest()->getPost('source', 1);
            $submitSku = $skuList ? json_decode($skuList, true) : [];
        }

        $skuToken = $this->getRequest()->getPost('sku_token', '');
        $provinceCode = $this->getRequest()->getPost('province', '');
        $cityId = $this->getRequest()->getPost('city_id', '');
        $acceptId = $this->getRequest()->getPost('accept_id', '');
        $couponId = $this->getRequest()->getPost('coupon_id', null);
        $couponVid = $this->getRequest()->getPost('coupon_vid', null);
        $sCouponInfoStr = $this->getRequest()->getPost('s_coupon_info', null);
        $mealId = $this->getRequest()->getPost('meal_id', 0);
        $useIntegral = $this->getRequest()->getPost('use_integral', 1);

        $storeId = $this->getRequest()->getPost('store_id', 0);
        $isSelfMention = $this->getRequest()->getPost('self_mention', 0);

        $activityId = $this->getRequest()->getPost('activity_id', 0);
        $activityType = $this->getRequest()->getPost('activity_type', 0);
        $teamJoinId = $this->getRequest()->getPost('team_join_id', 0);
        $sCouponInfoArr = $sCouponInfoStr ? json_decode($sCouponInfoStr, true) : null;
        $usePacket = $this->getRequest()->getPost('use_packet', 1);
        $sCouponInfo = null;
        if ($sCouponInfoArr) {
            foreach ($sCouponInfoArr as $row) {
                $sCouponInfo[$row['shop_id']] = $row['coupon_info'];
            }
        }
        $areaCode = $cityId ? $cityId : $provinceCode;



        $couponId = $couponId ? $couponId : null;

        \Doraemon\tools\Log::order_createOrder_info('确认订单', compact('couponId', 'submitSku', 'skuToken', 'json_param'));

        if (!$submitSku && !$skuToken) {
            throw new Exception('参数错误');
        }

        $list = OrderService::confirmOrderList($this->uid, $submitSku, $areaCode, $source, $acceptId, $skuToken, $couponId, $couponVid, $sCouponInfo, $mealId, $useIntegral, $usePacket, $this->appType, $storeId, $isSelfMention, $activityId, $activityType, $teamJoinId);


        ActionLog::log($this->uid, [], ActionLog::BIZ_ORDER_COMFRIM);
        $this->setSuccess($list);
    }
}
